package com.mingyuan.demo2;

import com.alibaba.nacos.api.naming.NamingFactory;
import com.alibaba.nacos.api.naming.NamingService;
import net.bytebuddy.implementation.bind.annotation.Origin;
import net.bytebuddy.implementation.bind.annotation.RuntimeType;
import net.bytebuddy.implementation.bind.annotation.SuperCall;

import java.lang.reflect.Method;
import java.net.InetAddress;
import java.util.concurrent.Callable;


public class NacosRegister {

    @RuntimeType
    public static Object intercept(@Origin Method method, @SuperCall Callable<?> callable) throws Exception {
            // 获取本地主机IP
            String ip = InetAddress.getLocalHost().getHostAddress();
            // 创建并初始化NamingService
            NamingService namingService = NamingFactory.createNamingService("150.158.78.193:8848");
            // 执行Nacos注册逻辑
            namingService.registerInstance("nacos-ByteBuddy-demo", ip, 8082);
            System.out.println(ip + " 已成功注册到Nacos！");
            // 原有函数执行
            return callable.call();
    }

}
